package chs;

import static org.junit.Assert.assertTrue;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.testing.RobotTestBed;
import robocode.control.snapshot.IBulletSnapshot;

/**
 * Tests that Atom fires at max power when enemy is close.
 *
 * @author Sean Chung
 */
public class TestAtomCharging extends RobotTestBed {
  // Check that power is constant
  boolean maxPower;

  /**
   * Specifies that SittingDuck and Atom are to be matched up in this test case.
   *
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,chs.Atom";
  }

  /**
   * This test runs for 10 round.
   *
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Check that all bullets fired are at power 3.0, since SittingDuck does not fire,
   * all bullets should be from Atom.
   *
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();
    double bulletPower;

    // All bullets but last should be of power 3.0 since SittingDuck is close. 
    for (int i = 0; i < (bullets.length - 1); i++) {
      bulletPower = bullets[i].getPower();
      if (bulletPower == 3.0)  {
        maxPower = true;
      }
      else { 
        maxPower = false;
        break;
      }
    }    
  }
  
  
  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the 
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 
   * has position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases. 
   * 
   * Defaults to null, which means that the initial positions are determined randomly.  Since
   * battles are deterministic by default, the initial positions are randomly chosen but will
   * always be the same each time you run the test case. 
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return "0,0,90, 5,0,0";
  }

  /**
   * After the battle, check to see that successfully charged.
   *
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {    
    BattleResults[] battleResults = event.getIndexedResults();
    // Sanity check that results[1] is Atom (not strictly necessary, but illustrative).
    BattleResults AtomResults = battleResults[1];
    double ramDamage = AtomResults.getRamDamage();
    assertTrue("Max Power", maxPower);
    // Make sure Atom does at least some ramming damage
    assertTrue("Rammed", ramDamage >= 100);
    
  }
}
